/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.EclipseSearchVeiwWrapper;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.FileTextSearchScope;
import org.eclipse.search.ui.text.TextSearchQueryProvider;
import org.eclipse.search.ui.text.TextSearchQueryProvider.TextSearchInput;
import org.eclipse.ui.PlatformUI;

import com.googlecode.umlsynch.eclipse.rpc.Debug.TEclipseDebug;
import com.googlecode.umlsynch.rpc.TSearchQueryDescriptor;
import com.googlecode.umlsynch.rpc.TSearchType;
import com.googlecode.umlsynch.rpc.*;

public class EclipseSeachQuery {
	private TSearchQueryDescriptor desc = null;
	private TEclipseDebug debug = new TEclipseDebug("SQ");
	
	public EclipseSeachQuery(TSearchQueryDescriptor desc2) {
		desc = desc2;
	}

	private ISearchQuery newQuery() throws CoreException {
		TextSearchPageInput input = 
			new TextSearchPageInput(desc.text, 
					desc.isCaseSensitive, desc.isRegExSearch, createTextSearchScope());

		return TextSearchQueryProvider.getPreferred().createQuery(input);
	}
	
	public boolean performAction() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				try {
					NewSearchUI.runQueryInBackground(newQuery());
				} catch (CoreException e) {
					//ErrorDialog.openError(getShell(), SearchMessages.TextSearchPage_replace_searchproblems_title, SearchMessages.TextSearchPage_replace_searchproblems_message, e.getStatus());
				}
			}});

		return true;
	}
	
	public FileTextSearchScope createTextSearchScope() {
		// Setup search scope
		
		if (desc.type == TSearchType.WORKSPACE_SCOPE) {
			debug.Note("WORKSPACE_SCOPE");
			return FileTextSearchScope.newWorkspaceScope(desc.patterns, desc.fSearchDerived);
		}
		if (desc.type == TSearchType.SELECTION_SCOPE) {
			debug.Note("SELECTION_SCOPE");
			return getSelectedResourcesScope();
		}
		
		if (desc.type == TSearchType.SELECTED_PROJECTS_SCOPE) {
			debug.Note("PROJECT_SCOPE");
			return getEnclosingProjectScope();
		}
		debug.Note("Workspace scope is default");
       /*
		if (desc.SearchType == VPSDESearchQueryDescription.TSearchType.WORKING_SET_SCOPE) {
			IWorkingSet[] workingSets= getContainer().getSelectedWorkingSets();
			return FileTextSearchScope.newSearchScope(workingSets, getExtensions(), fSearchDerived);
		}*/
		return FileTextSearchScope.newWorkspaceScope(desc.patterns, desc.fSearchDerived);
	}	
	

	private FileTextSearchScope getSelectedResourcesScope() {
		String [] enclosingProjectName = new String[1];
		enclosingProjectName[0] = desc.projectName; 
		
		debug.Note("PN: " + desc.projectName);
		
		if (enclosingProjectName == null) {
			return FileTextSearchScope.newWorkspaceScope(desc.patterns, desc.fSearchDerived);
		}

		IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
		IResource[] res= null;
		if ((desc.paths != null) && (desc.paths.length != 0)) {
			res = new IResource[desc.paths.length];
			for (int i= 0; i < desc.paths.length; i++) {
				res[i] = root.findMember(desc.projectName + "/" + desc.paths[i]);
				debug.Note("SP: " + desc.paths[i]);
			}
		} else { 
			res = new IResource[1];
			res[0] = root.findMember(desc.projectName);
			debug.Note("SP: " + desc.projectName);
		}


		return FileTextSearchScope.newSearchScope(res, desc.patterns, desc.fSearchDerived);
	}

	private FileTextSearchScope getEnclosingProjectScope() {
		
		String [] enclosingProjectName = new String[1];
		enclosingProjectName[0] = desc.projectName; 
		debug.Note("PN: " + desc.projectName);		
		if (enclosingProjectName == null) {
			return FileTextSearchScope.newWorkspaceScope(desc.patterns, desc.fSearchDerived);
		}

		IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
		IResource[] res= new IResource[enclosingProjectName.length];
		for (int i= 0; i < res.length; i++) {
			res[i]= root.getProject(enclosingProjectName[i]);
			debug.Note("SP: " + desc.projectName);
		}
		debug.Note("SP: " + desc.projectName);
		return FileTextSearchScope.newSearchScope(res, desc.patterns, desc.fSearchDerived);
	}

}


class TextSearchPageInput extends TextSearchInput {

	private final String  fSearchText;
	private final boolean fIsCaseSensitive;
	private final boolean fIsRegEx;
	private final FileTextSearchScope fScope;

	public TextSearchPageInput(String  searchText, boolean isCaseSensitive, boolean isRegEx, FileTextSearchScope scope) {
		fSearchText= searchText;
		fIsCaseSensitive= isCaseSensitive;
		fIsRegEx= isRegEx;
		fScope= scope;
	}

	public String  getSearchText() {
		return fSearchText;
	}

	public boolean isCaseSensitiveSearch() {
		return fIsCaseSensitive;
	}

	public boolean isRegExSearch() {
		return fIsRegEx;
	}

	public FileTextSearchScope getScope() {
		return fScope;
	}
}

	
